//
//  GoodsInfoTopView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/17.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "GoodsInfoTopView.h"
#import "UIImage+Color.h"
#import "GoodsInfoEntity.h"
#import <YBImageBrowser/YBImageBrowser.h>

@interface GoodsInfoTopView ()<SDCycleScrollViewDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) UILabel *goodsNameLab;
@property (nonatomic, strong) UILabel *goodsDetailLab;
@property (nonatomic, strong) UILabel *soldNumLab;
@property (nonatomic, strong) UILabel *vipPriceLab;
@property (nonatomic, strong) UILabel *priceLab;

@property (nonatomic, strong) UILabel *consumerInfoTitle;
@property (nonatomic, strong) UIImageView *consumerLine;
@property (nonatomic, strong) UILabel *consumerNameLab;
@property (nonatomic, strong) UILabel *consumerTelLab;
@property (nonatomic, strong) UILabel *consumerAddrLab;
@property (nonatomic, strong) UILabel *consumerAddr;
@property (nonatomic, strong) UIButton *consumerAction;

@property (nonatomic, strong) UILabel *goodsInfoTitle;
@property (nonatomic, strong) UIImageView *goodsLine;

@property (nonatomic, strong) NSArray<NSString *> *imageUrlStringArray;

@end

@implementation GoodsInfoTopView

- (void)setData:(GoodsInfoTotalEntity *)data
{
    _data = data;
    self.goodsNameLab.text = String(data.goods.goodsName);
    self.goodsDetailLab.text = String(data.goods.summary);
    self.soldNumLab.text = [NSString stringWithFormat:@"已售%@份",data.goods.soldNum ?: @"0"];
    self.consumerNameLab.text = String(data.goods.consumer.orgName);
    self.consumerTelLab.text = [NSString stringWithFormat:@"电话：%@",String(data.goods.consumer.telephone)];
    self.consumerAddr.text = String(data.goods.consumer.address);
    NSString *price = [NSString stringWithFormat:@"原价：￥%@",data.goods.price];
    NSMutableAttributedString *priceAttri = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttri setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:kColor888888} range:NSMakeRange(0, 3)];
    [priceAttri setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:kColor333333} range:NSMakeRange(3, price.length-3)];
    self.priceLab.attributedText = priceAttri;
    #warning 会员价显示控制
//    if (UserUtils.shared.showVip) {
        NSString *vipPrice = [NSString stringWithFormat:@"会员价：￥%@",StringWithPlaceHolder(data.goods.memberPrice, @"0")];
        NSMutableAttributedString *vipPriceAttri = [[NSMutableAttributedString alloc] initWithString:vipPrice];
        [vipPriceAttri setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:kColor888888} range:NSMakeRange(0, 4)];
        [vipPriceAttri setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:kColorFF2C2C} range:NSMakeRange(4, vipPrice.length-4)];
        self.vipPriceLab.attributedText = vipPriceAttri;
//    }
    
    
    NSMutableArray *temp = @[].mutableCopy;
    for (NSDictionary *dict in data.fileList) {
        NSString *filePath = dict[@"filePath"];
        [temp addObject:[NSString stringWithFormat:@"%@%@",SERVERADDRS,filePath]];
    }
    self.imageUrlStringArray = temp;
    self.cycleScrollView.imageURLStringsGroup = temp;
}

- (void)setupSubViews
{
    [self addSubview:self.cycleScrollView];
    
    self.goodsNameLab = [[UILabel alloc] init];
    self.goodsNameLab.font = [UIFont boldSystemFontOfSize:15];
    self.goodsNameLab.textColor = kColor333333;
    self.goodsNameLab.numberOfLines = 0;
    [self addSubview:self.goodsNameLab];
    
    self.goodsDetailLab = [[UILabel alloc] init];
    self.goodsDetailLab.font = [UIFont systemFontOfSize:12];
    self.goodsDetailLab.textColor = kColor666666;
    self.goodsDetailLab.numberOfLines = 0;
    [self addSubview:self.goodsDetailLab];
    
    self.soldNumLab = [[UILabel alloc] init];
    self.soldNumLab.font = [UIFont systemFontOfSize:12];
    self.soldNumLab.textColor = UIColorFromRGB(0x287EFF);
    self.soldNumLab.text = @"已售0份";
    [self addSubview:self.soldNumLab];
    
    self.vipPriceLab = [[UILabel alloc] init];
    [self addSubview:self.vipPriceLab];
    
    self.priceLab = [[UILabel alloc] init];
    [self addSubview:self.priceLab];
    
    self.consumerInfoTitle = [[UILabel alloc] init];
    self.consumerInfoTitle.font = [UIFont boldSystemFontOfSize:15];
    self.consumerInfoTitle.textColor = kColor333333;
    self.consumerInfoTitle.text = @"商家信息";
    [self addSubview:self.consumerInfoTitle];
    
    self.consumerLine = [[UIImageView alloc] init];
    self.consumerLine.image = [UIImage gradientImageWithColorArray:@[(id)kColorGradientBegin.CGColor,(id)kColorGradientEnd.CGColor] beginPoint:CGPointMake(0, 0) endPoint:CGPointMake(30, 0) rect:CGRectMake(0, 0, 30, 2)];
    [self addSubview:self.consumerLine];
    
    self.consumerNameLab = [[UILabel alloc] init];
    self.consumerNameLab.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
    self.consumerNameLab.textColor = kColor333333;
    self.consumerNameLab.numberOfLines = 0;
    [self addSubview:self.consumerNameLab];
    
    self.consumerTelLab = [[UILabel alloc] init];
    self.consumerTelLab.font = [UIFont systemFontOfSize:13];
    self.consumerTelLab.textColor = kColor333333;
    self.consumerTelLab.text = @"电话：";
    [self addSubview:self.consumerTelLab];
    
    self.consumerAddrLab = [[UILabel alloc] init];
    self.consumerAddrLab.font = [UIFont systemFontOfSize:13];
    self.consumerAddrLab.textColor = kColor333333;
    self.consumerAddrLab.text = @"地址：";
    [self addSubview:self.consumerAddrLab];
    
    self.consumerAddr = [[UILabel alloc] init];
    self.consumerAddr.font = [UIFont systemFontOfSize:13];
    self.consumerAddr.textColor = kColor333333;
    self.consumerAddr.numberOfLines = 0;
    [self addSubview:self.consumerAddr];
    
    self.consumerAction = [[UIButton alloc] init];
    self.consumerAction.backgroundColor = [UIColor clearColor];
    [self.consumerAction addTarget:self action:@selector(gotoConsumerPage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.consumerAction];
    
    if (self.showGoodsInfoTitle) {
        
    }
    
    
//    self.goodsNameLab.text = @"韩式小气泡深层清洁";
//    self.goodsDetailLab.text = @"深层清洁 毛孔净化 同时补水保湿美白";
//    self.consumerNameLab.text = @"丽人美容美体中心";
//    self.consumerTelLab.text = @"电话：075-89117777";
//    self.consumerAddr.text = @"深圳市宝安区新湖路236号二楼";
    
}

- (void)setupConstraints
{
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(@(180.0/375*kScreenW));
    }];
    
    [self.goodsNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cycleScrollView.mas_bottom).offset(23);
        make.left.equalTo(self).offset(16);
        make.right.lessThanOrEqualTo(self).offset(-16);
    }];
    
    [self.goodsDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsNameLab.mas_bottom).offset(12);
        make.left.equalTo(self.goodsNameLab);
        make.right.lessThanOrEqualTo(self).offset(-16);
    }];
    
    [self.soldNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsDetailLab.mas_bottom).offset(35);
        make.left.equalTo(self.goodsNameLab);
    }];
    
    [self.priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsDetailLab.mas_bottom).offset(12);
        make.right.equalTo(self).offset(-15);
    }];
    
    [self.vipPriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLab.mas_bottom).offset(5);
        make.right.equalTo(self).offset(-15);
    }];
    
    [self.consumerInfoTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodsDetailLab.mas_bottom).offset(73);
        make.centerX.equalTo(self);
    }];
    
    [self.consumerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumerInfoTitle.mas_bottom).offset(7);
        make.centerX.equalTo(self);
        make.width.equalTo(@30);
        make.height.equalTo(@2);
    }];
    
    [self.consumerNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumerInfoTitle.mas_bottom).offset(38);
        make.left.equalTo(self.goodsNameLab);
    }];
    
    [self.consumerTelLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumerNameLab.mas_bottom).offset(14);
        make.left.equalTo(self.goodsNameLab);
    }];
    
    [self.consumerAddrLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.consumerAddrLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumerTelLab.mas_bottom).offset(14);
        make.left.equalTo(self.goodsNameLab);
    }];
    [self.consumerAddr setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.consumerAddr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.consumerAddrLab);
        make.left.equalTo(self.consumerAddrLab.mas_right);
        make.right.lessThanOrEqualTo(self).offset(-16);
        make.bottom.equalTo(self).offset(-16).priority(200);
    }];
    
    [self.consumerAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.consumerNameLab);
        make.bottom.equalTo(self.consumerAddr);
    }];
    
    if (self.showGoodsInfoTitle) {
        
    }
    
    
}

- (void)setShowGoodsInfoTitle:(BOOL)showGoodsInfoTitle
{
    _showGoodsInfoTitle = showGoodsInfoTitle;
    if (showGoodsInfoTitle) {
        self.goodsInfoTitle = [[UILabel alloc] init];
        self.goodsInfoTitle.font = [UIFont boldSystemFontOfSize:15];
        self.goodsInfoTitle.textColor = kColor333333;
        self.goodsInfoTitle.text = @"商品详情";
        [self addSubview:self.goodsInfoTitle];
        
        self.goodsLine = [[UIImageView alloc] init];
        self.goodsLine.image = [UIImage gradientImageWithColorArray:@[(id)kColorGradientBegin.CGColor,(id)kColorGradientEnd.CGColor] beginPoint:CGPointMake(0, 0) endPoint:CGPointMake(30, 0) rect:CGRectMake(0, 0, 30, 2)];
        [self addSubview:self.goodsLine];
        
        [self.goodsInfoTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.consumerAddr.mas_bottom).offset(50);
            make.centerX.equalTo(self);
        }];
        
        [self.goodsLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.goodsInfoTitle.mas_bottom).offset(7);
            make.centerX.equalTo(self);
            make.width.equalTo(@30);
            make.height.equalTo(@2);
            make.bottom.equalTo(self).offset(-16);
        }];
    }
}

- (void)gotoConsumerPage
{
    if (self.gotoConsumerPageHandle) {
        self.gotoConsumerPageHandle();
    }
}

- (void)browserImage:(NSInteger)index
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = index;
    [browser show];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    [self browserImage:index];
}

- (SDCycleScrollView *)cycleScrollView
{
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"pic_moren"]];
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    }
    return _cycleScrollView;
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return self.imageUrlStringArray.count;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.url = [NSURL URLWithString:self.imageUrlStringArray[index]];
    data.sourceObject = self.cycleScrollView;
    return data;
}

@end
